package com.zbbt.sportslottery.ui.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.bean.IndexFunBean;
import com.zbbt.sportslottery.util.LogUtil;

public class GridViewAdapter extends BaseAdapter {
	private List<IndexFunBean> picObjects = null;
	private Context context;
	private LayoutInflater inflater;
	private kjTextPicView textPicView;
	private int[] picStrings = { R.drawable.kj_fenxi, R.drawable.kj_ditu, R.drawable.kj_shuju,
			R.drawable.kj_huodong };

	public GridViewAdapter(Activity context, List<IndexFunBean> picObjects) {
		this.context = context;
		this.picObjects = picObjects;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		LogUtil.printD("GridViewAdapter", "getCount" + picObjects.size() + "");
		return picObjects.size();
	}

	@Override
	public Object getItem(int position) {
		LogUtil.printD("GridViewAdapter", "getItem" + picObjects.get(position) + "");
		return picObjects.get(position);
	}

	@Override
	public long getItemId(int position) {
		LogUtil.printD("GridViewAdapter", "getItemID" + position + "");
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_kj_four, null);
			textPicView = new kjTextPicView();
			textPicView.kjTitleText = (TextView) convertView.findViewById(R.id.kj_fenxi);
			textPicView.kjContent1 = (TextView) convertView.findViewById(R.id.kj_content1);
			textPicView.kjContext2 = (TextView) convertView.findViewById(R.id.kj_content2);
			textPicView.kjPicView = (ImageView) convertView.findViewById(R.id.kj_pic);
			textPicView.linView = (ImageView) convertView.findViewById(R.id.line_image);
			textPicView.viewLine = convertView.findViewById(R.id.line);
			convertView.setTag(textPicView);
		} else {
			textPicView = (kjTextPicView) convertView.getTag();
		}
		IndexFunBean indexFunBean = picObjects.get(position);
        
		if (!TextUtils.isEmpty(indexFunBean.getFunName())) {
			textPicView.kjTitleText.setText(indexFunBean.getFunName());
		} else {
			switch (indexFunBean.getFunType()) {
			case 0:
				textPicView.kjTitleText.setText("图表走势");
				break;
			case 1:
				textPicView.kjTitleText.setText("网点地图");
				break;
			case 2:
				textPicView.kjTitleText.setText("赛事数据");
				break;
			case 3:
				textPicView.kjTitleText.setText("最新活动");
				break;
			default:
				break;
			}

		}

		if (!TextUtils.isEmpty(indexFunBean.getFunDesFirst())) {
			textPicView.kjContent1.setText(indexFunBean.getFunDesFirst());
		}
		if (!TextUtils.isEmpty(indexFunBean.getFunDesSecond())) {
			textPicView.kjContext2.setText(indexFunBean.getFunDesSecond());
			
			if (position==3) {
				textPicView.kjContext2.setTextColor(context.getResources().getColor(R.color.white));
				textPicView.kjContext2.setBackgroundResource(R.drawable.huodong_pic);
				if (indexFunBean.getFunDesSecond().length()>6) {
					textPicView.kjContext2.setPadding(10, 0, 10, 0);
				}else {
					textPicView.kjContext2.setPadding(20, 0, 20, 0);
				}
			}
		}
		if (position<picStrings.length) {
			textPicView.kjPicView.setBackgroundResource(picStrings[position]);
		}

		if (position == 1 || position == 3) { 
			textPicView.linView.setVisibility(View.GONE);
		}
		if (position >= picObjects.size() / 2) {
			// 最后一行分割线隐藏
			textPicView.viewLine.setVisibility(View.GONE);
		}
		return convertView;
	}

	class kjTextPicView {
		TextView kjTitleText;
		TextView kjContent1;
		TextView kjContext2;
		ImageView kjPicView;
		ImageView linView;
		View viewLine;
	}

}
